package com.kun.framework.queue.core;

import com.kun.framework.data.persistence.JsonDataPersistence;
import com.kun.framework.data.persistence.rule.FileCreateRule;

import java.io.File;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 默认抽象消息队列
 *
 * @author gzc
 * @since 2024/8/21
 **/
public abstract class DefaultAbstractMessageQueue<V> extends JsonDataPersistence<ConcurrentLinkedQueue<V>> implements MessageQueue<V> {
    /**
     * 存储文件基础目录
     */
    protected static String baseStorageDir = System.getProperty("user.dir") + File.separator + "storage" + File.separator + "queue";
    /**
     * 缓存数据是否有更新过
     */
    protected volatile boolean isDataUpdate = false;
    /**
     * 线程安全的非阻塞单向队列
     */
    protected final ConcurrentLinkedQueue<V> QUEUE = new ConcurrentLinkedQueue<>();

    public DefaultAbstractMessageQueue(FileCreateRule fileCreateRule) {
        super(baseStorageDir, fileCreateRule);
    }

    @Override
    public ConcurrentLinkedQueue<V> getDataObj() {
        return QUEUE;
    }

    @Override
    public void destroy() {

    }

    @Override
    public void add(V message) {
        if (message != null) {
            QUEUE.add(message);
            isDataUpdate = true;
        }
    }

    @Override
    public V remove() {
        return QUEUE.poll();
    }
}
